package com.siyu.preplan.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.siyu.basedata.plan.domain.Plan;
import com.siyu.basedata.plan.service.IPlanService;
import com.siyu.common.core.controller.BaseController;
import com.siyu.common.core.domain.AjaxResult;
import com.siyu.common.utils.uuid.IdUtils;
import com.siyu.predict.dto.PlanDto;
import com.siyu.preplan.dto.PlanPDepotDto;
import com.siyu.preplan.dto.PlanPrePlanDto;
import com.siyu.preplan.dto.PrePlanDto;
import com.siyu.preplan.planpcondition.service.impl.PlanPConditionServiceImpl;
import com.siyu.preplan.planpdocument.service.impl.PlanPDocumentServiceImpl;
import com.siyu.preplan.planpmaterial.service.impl.PlanPMaterialServiceImpl;
import com.siyu.preplan.planpplan.service.impl.PlanPPlanServiceImpl;
import com.siyu.preplan.service.PlanPeService;
import com.siyu.preplan.service.PlanPnewService;
import com.siyu.preplan.service.impl.PlanPeServiceImpl;
import com.siyu.preplan.service.impl.PlanPnewServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/preplan/new")
public class PlanPnewController extends BaseController {

    private static final String LOCK_KEY_PREFIX = "lock:";
    private static final int LOCK_EXPIRE_SECONDS = 30; // 锁的过期时间

    @Autowired
    private IPlanService planService;

    @Autowired
    private PlanPMaterialServiceImpl planPMaterialService;

    @Autowired
    private PlanPDocumentServiceImpl planPDocumentService;

    @Autowired
    private PlanPConditionServiceImpl planPConditionService;

    @Autowired
    private PlanPPlanServiceImpl planPPlanService;

    @Autowired
    private PlanPnewService planPnewService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private PlanPeService planPeService;

    /**
     * 新建预案
     *
     * @param prePlanDto
     * @return
     */
    @PostMapping("/plan/createplan")
    public AjaxResult insertPrePlan(@RequestBody PrePlanDto prePlanDto) {
        LinkedHashMap<String, String> planPPlanIds = null;
        try {
            // 判断是否传递了主键id过来
            if (prePlanDto.getId() != null) {
                List<String> ids = new ArrayList<>();
                ids.add(prePlanDto.getId());
                // 先删除这个预案
                planPeService.delete(ids);
            }
            planPPlanIds = planPnewService.insertPrePlan(prePlanDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存失败!");
        }
        return AjaxResult.success(planPPlanIds);
    }

    ;


    /**
     * 上传新建预案的地图geojson
     *
     * @param safe
     * @param danger
     * @param aftercare
     * @param route
     * @param id
     * @return
     */
    @PostMapping(value = "/plan/upload", consumes = "multipart/form-data")
    public AjaxResult uploadMapFile(@RequestParam(value = "safe",required=false) MultipartFile safe,
                                    @RequestParam(value = "danger",required=false) MultipartFile danger,
                                    @RequestParam(value = "aftercare",required=false) MultipartFile aftercare,
                                    @RequestParam(value = "route",required=false) MultipartFile route,
                                    @RequestParam(value = "matter",required=false) MultipartFile matter,
                                    @RequestParam("id")String id) throws IOException {
            return planPnewService.uploadMapFile(safe, danger, aftercare, route,matter,id);

    }

    /**
     * 新建仓库和物资信息
     * @param planPDepotDto
     * @return
     */
    @PostMapping("/plan/createdepot")
    public AjaxResult createDepot(@RequestBody PlanPDepotDto planPDepotDto) {
        try {
            planPnewService.createDepot(planPDepotDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新建失败!");
        }
        return AjaxResult.success("新建成功!");
    }


}
